/*
 *  Copyright (C) 2011 yudi wibisono (yudi1975@gmail.com)
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.upi.cs;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.logging.Logger;

/**
 *
 * @author yudi wibisono
 */
public class Prepro {



    public static void cariSessionOrder(String inputFile, String transOrderFile) {
        //dikerjakan setelah cariTransOrder dipanggil
        //input: input file->data mentah, transOrderFile --> file berisi trans order
        //output: semua trans (view,basket,order) pada session yang mengandung order

      HashSet<String> hsSession = new HashSet<String>();

      Logger log = Logger.getLogger("edu.cs.upi.tweetmining");
      String inputFileWoExt = inputFile.substring(0, inputFile.lastIndexOf('.')); //without ext
      String namaFileOutput = inputFileWoExt+"_sessionwithtransOrder.txt";
      
      try {
            FileInputStream fstream = new FileInputStream(transOrderFile);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int cc=0;
            while ((strLine = br.readLine()) != null)   {
                cc++;
                System.out.println(cc);
                String[] temp;
                temp = strLine.split("\\|");
                hsSession.add(temp[0]);
            }
            br.close();
            in.close();
            fstream.close();
            } catch (Exception e) {
                log.severe(e.toString());
       }

      //hsSession sudah terisi, ambil hantya session yg ada dihsSession
      try {
            PrintWriter pw = new PrintWriter(namaFileOutput);
            FileInputStream fstream = new FileInputStream(inputFile);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int cc=0;
            while ((strLine = br.readLine()) != null)   {
                cc++;
                System.out.println(cc);
                String[] temp;
                temp = strLine.split("\\|");
                
                if (hsSession.contains(temp[0]))  //sesinya mengandung transaski order
                {
                    pw.println(strLine);
                }

                if ( (cc % 10000) ==0) {pw.flush();}
            }
            pw.close();
            br.close();
            in.close();
            fstream.close();
            } catch (Exception e) {
                log.severe(e.toString());
       }

    }

    public static void cariTransOrder(String inputFile) {
        //input adalah file mentah task 1
        //output adalah trans yang mengandung trankasi bertipe 2 (order)
      Logger log = Logger.getLogger("edu.cs.upi.tweetmining");
      String inputFileWoExt = inputFile.substring(0, inputFile.lastIndexOf('.')); //without ext
      String namaFileOutput = inputFileWoExt+"_transOrder.txt";
      try {
            PrintWriter pw = new PrintWriter(namaFileOutput);
            FileInputStream fstream = new FileInputStream(inputFile);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int cc=0;
            while ((strLine = br.readLine()) != null)   {
                cc++;
                System.out.println(cc);
                String[] temp;
                temp = strLine.split("\\|");
                if (temp[2].equals("2"))
                {
                    pw.println(strLine);  
                }
                if ( (cc % 10000) ==0) {pw.flush();}
            }
            pw.close();
            br.close();
            in.close();
            fstream.close();
            } catch (Exception e) {
                log.severe(e.toString());
       }
    }

    public static void main(String[]args) {
        //testing
        //Prepro.cariTransOrder("F:\\dataminingcup\\task1\\transact_train.txt");
        Prepro.cariSessionOrder("F:\\dataminingcup\\task1\\transact_train.txt","F:\\dataminingcup\\task1\\transact_train_transtype2.txt");
    }
}
